package com.qf.qflivehou.service.serviceImpl;

import com.qf.qflivehou.config.RedisKey;
import com.qf.qflivehou.mapper.repository.RotationRepository;
import com.qf.qflivehou.pojo.respVo.ResponseData;
import com.qf.qflivehou.pojo.vo.RotationImage;
import com.qf.qflivehou.service.RotationService;
import org.hibernate.annotations.common.util.impl.LoggerFactory;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Mr.Liu
 * @date 2022/1/10 21:43
 */
@Service
public class RotationServiceImpl implements RotationService {
    @Autowired
    private RotationRepository rotationRepository;
    @Autowired
    private RedisTemplate redisTemplate;
    //声明日志
    private static Logger logger = LoggerFactory.logger(QfTeachServiceImpl.class);

    @Override
    public ResponseData findRotation(String status) {
        //1.判断redis种是否有该key值
        Boolean aBoolean = redisTemplate.hasKey(RedisKey.ROTATION_KEY + status);
        if(aBoolean){
            //有该key值,直接从redis中获取
            logger.debug("进入了redis查询");
            List list = redisTemplate.opsForList().range(RedisKey.ROTATION_KEY + status, 0, -1);
            return new ResponseData(0,"查询成功",list,null);
        }else {
            logger.debug("进入了数据查询");
            //代表redis中没有该key，则需要从数据库先来进行查询。将结果集放置在Redis中
            List<RotationImage> list = rotationRepository.findByStatusOrderByImageIndex(status);
            if (list!=null){
                //将查询出的结果集放置在redis中
                redisTemplate.opsForList().leftPushAll(RedisKey.ROTATION_KEY+status,list);
                return new ResponseData(0,"查询成功",list,null);
            }else {
                return new ResponseData().fail();
            }
        }
    }
}
